import EchartsView from "@/components/EchartsView";
import OPS from "./options";
import {
  getDeviceCount,
  getAlarmEventCountOnDate,
  getDeviceCountOnDateAndModel,
  getAlarmEvent
} from "@/api/monitoring/monitoring";
import { get } from "sortablejs";
export default {
  components: { EchartsView },

  // 监听$store.state.companyArr
  watch: {
    "$store.state.companyArr": {
      deep: true,
      handler: function (newVal) {
        this.getDeviceData();
        this.getAlarmData();
        this.getAlarmTable()
        this.getPieData()
        this.getYearDevices()
      },
    },
  },
  props: {},

  data() {
    return {
      deviceInfo: {},
      chart: null,
      tableData: [],
      isTableLoading: false,
      tableType:'DEVICE_ALARM',
      alarmEchartType:1,
      alarmPieType:1,
      alarmYearType:1
    };
  },
  mounted() {
    this.getDeviceData();
    this.getAlarmData();
    this.getAlarmTable()
    this.getPieData()
    this.getYearDevices()
  },
  beforeDestroy() {},
  methods: {
    // 获取数据
    async getDeviceData() {

      let str = "";
      for (let i = 0; i < this.$store.state.companyArr.arr.length; i++) {
        str = str + this.$store.state.companyArr.arr[i];
        if(i != this.$store.state.companyArr.arr.length-1){
          str = str+",";
        }
      }
      let res = await getDeviceCount({
        pOrgId: str,
      });
      this.deviceInfo = res.data;
      this.isTableLoading = false;
    },
    // 选择告警时间类型
    choseEchartTime(e){
      this.alarmEchartType = e
      this.getAlarmData()
    },
       // 选择告警时间类型
       chosePieTime(e){
        this.alarmPieType = e
      this.getPieData()
      },
      // 全年设备
      choseYearTime(e){
        this.alarmYearType = e
      this.getYearDevices()
      },
    // 告警情况数据
    async getAlarmData() {
      // 遍历companyArr中的id组成字符串
      let str = "";
      for (let i = 0; i < this.$store.state.companyArr.arr.length; i++) {
        str = str + this.$store.state.companyArr.arr[i]
        if(i != this.$store.state.companyArr.arr.length-1){
          str = str+",";
        }
      }
      let res = await getAlarmEventCountOnDate({
        pOrgId: str,
        dateType: this.alarmEchartType,
      });
      OPS.options.alarmData.xAxis.data = res.data.map(
        (item) => item.x
      );
      OPS.options.alarmData.series[0].data = res.data.map(
        (item) => item.y
      );
      this.$refs.chart1 &&
        this.$refs.chart1.handleSetChart(OPS.options.alarmData);
    },
    // 告警情况表格类型
    choseTableType(e){
      this.tableType = e
      this.getAlarmTable()
    },
    // 告警情况表格
    async getAlarmTable(){
      let str = "";
      for (let i = 0; i < this.$store.state.companyArr.arr.length; i++) {
        str = str + this.$store.state.companyArr.arr[i]
        if(i != this.$store.state.companyArr.arr.length-1){
          str = str+",";
        }
      }
      let res = await getAlarmEvent({
        pOrgId: str,
        dateType: 1,
        noticeAction: this.tableType
      })
      this.tableData = res.data
    },
    // 全年数据分析饼图
    async getPieData(){
       // 遍历companyArr中的id组成字符串
       let str = "";
       for (let i = 0; i < this.$store.state.companyArr.arr.length; i++) {
         str = str + this.$store.state.companyArr.arr[i]
         if(i != this.$store.state.companyArr.arr.length-1){
           str = str+",";
         }
       }
       let res = await getDeviceCount({
        pOrgId: str,
      });
       
       OPS.options.yearData.series[1].data =[
        {name:'正常',value:res.data.normal}
        ,{name:'告警',value:res.data.abnormal}
        ,{name:'离线',value:res.data.offline},
        {name:'故障',value:res.data.fault}
       ]
       console.log(OPS.options.yearData.series[1].data)
       this.$refs.chart2 && this.$refs.chart2.handleSetChart(OPS.options.yearData);
    },
    // 全年设备数据分析
    async getYearDevices(){
     // 遍历companyArr中的id组成字符串
     let str = "";
     for (let i = 0; i < this.$store.state.companyArr.arr.length; i++) {
       str = str + this.$store.state.companyArr.arr[i]
       if(i != this.$store.state.companyArr.arr.length-1){
         str = str+",";
       }
     }
     let res = await getDeviceCountOnDateAndModel({
       pOrgId: str,
       dateType: this.alarmYearType,
     });
      OPS.options.yearDevices.xAxis.data = res.data.date;
      OPS.options.yearDevices.series[0].data = res.data.value[0].data;
    this.$refs.chart3 &&
    this.$refs.chart3.handleSetChart(OPS.options.yearDevices);
    }
  },
};
